import { defineStore } from 'pinia';
import {GetCart} from '@/api/index'
// type gtype = {num:number,id:number,name:string}
interface gtype {
    [propsName:string]:number|string
}
type goodType = {
    goods:gtype[]
}
export const useCarStore = defineStore("cart",{
    // state:():goodType=>({
    //     goods:[]
    // }),
    state:()=>({
        goods:[] as gtype[]
    }),
    getters:{
        totalNum:(state)=>{
            var n = 0;
            state.goods.forEach(item=>{
                if(typeof item.num ==="number"){
                    n += item.num
                }
               
            })
            return n;
        }
    },
    actions:{
        getCart(){
            GetCart()
            .then(res=>{
                this.goods = res.data.data;
            })
        }
    }
     

 })